package com.bltec.emergency.controller.resController;

import com.alibaba.fastjson.JSON;
import com.bltec.emergency.common.Constant;
import com.bltec.emergency.common.ResponseJson;
import com.bltec.emergency.model.ResShip;
import com.bltec.emergency.service.resService.ResShipService;
import com.bltec.emergency.service.mgrService.UserService;
import com.bltec.emergency.utils.DateUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/resShip")
@RequiresPermissions("project:res")
public class ResShipController {
    @Resource
    private ResShipService resShipService;
    @Resource
    private UserService userService;
    /**
     * 获得所有resShip
     */
    @RequestMapping("getAllResShip")
    public ResponseJson getResShip() {
        try {
            List<ResShip> ResShip = resShipService.getAll();
            return new ResponseJson(Constant.OK, JSON.toJSON(ResShip));
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseJson(Constant.FAIL, e.getMessage());
        }
    }

    /**
     * 添加resShip
     */
    @RequiresPermissions("res:insert")
    @RequestMapping("addResShip")
    public ResponseJson addResShip(String name, String owner, Long department, Long type, String state, String registry,String classificationSociety) {
        try {
            ResShip resShip = new ResShip();
            resShip.setName(name);
            resShip.setOwner(owner);
            resShip.setDepartment(department);
            resShip.setType(type);
            resShip.setState(state);
            resShip.setRegistry(registry);
            resShip.setClassificationSociety(classificationSociety);
            resShip.setEnable(true);
            resShip.setCreateBy(userService.getIdByUserName((String) SecurityUtils.getSubject().getPrincipal()));
            resShip.setCreateTime(DateUtil.dateNow());
            resShipService.add(resShip);
            return new ResponseJson(Constant.OK, "创建用户成功");
        } catch (Exception e) {
            return new ResponseJson(Constant.FAIL, "创建用户失败" + e.getMessage());
        }
    }

    /**
     * 修改resShip
     */
    @RequiresPermissions("res:update")
    @RequestMapping("updateResShip")
    public ResponseJson updateResShip(Long id, String name, String owner, Long department, Long type, String state, String registry,String classificationSociety) {
        try {
            ResShip resShip = resShipService.getResShipById(id);
            resShip.setName(name);
            resShip.setOwner(owner);
            resShip.setDepartment(department);
            resShip.setType(type);
            resShip.setState(state);
            resShip.setRegistry(registry);
            resShip.setClassificationSociety(classificationSociety);
            resShip.setUpdateBy(userService.getIdByUserName((String) SecurityUtils.getSubject().getPrincipal()));
            resShip.setUpdateTime(DateUtil.dateNow());
            resShipService.updateResShipById(resShip);
            return new ResponseJson(Constant.OK, JSON.toJSON(resShip));
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseJson.UPDATE_ERROR;
        }
    }

    /**
     * 删除resShip
     */
    @RequiresPermissions("res:delete")
    @RequestMapping("deleteResShip")
    public ResponseJson setDisable(Long id) {
        if (null != resShipService.getResShipById(id)) {
            if (resShipService.deleteResShipById(id)) {
                return new ResponseJson(Constant.OK, "删除成功");
            } else {
                return new ResponseJson(Constant.FAIL, "删除失败");
            }
        } else {
            return new ResponseJson(Constant.OK, "删除成功");
        }
    }
}
